﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Com.DA;
using Com.BU;
using System.Collections.ObjectModel;
using System.Media;

namespace Com.GUI.LibraryManager.Controls.SubControl
{
    /// <summary>
    /// Interaction logic for ucColdStorageEnthalpy.xaml
    /// </summary>
    public partial class ucColdStorageEnthalpy : UserControl
    {
        public ucColdStorageEnthalpy()
        {
            InitializeComponent();
            loadData();
        }

        public void loadData()
        {
            ObservableCollection<tb_product> proList = LibraryManagerInCom.ProductList;
            cbxDescription.ItemsSource = LibraryManagerInCom.ProductEnthalpyList;
            cbxDescription.DisplayMemberPath = "description";
            cbxDescription.SelectedIndex = 0;

            cbxProduct.ItemsSource = LibraryManagerInCom.ProductList;
            cbxProduct.DisplayMemberPath = "product_name";
            cbxProduct.SelectedIndex = 0;

        }

        public void reload()
        {
            cbxDescription.SelectedIndex = 0;
        }

        private void txtTemperature_Loaded(object sender, RoutedEventArgs e)
        {
            
        }

        private void cbxDescription_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            tb_enthalpy en = (tb_enthalpy)cbxDescription.SelectedItem;
            if (en != null)
            {
                cbxProduct.SelectedItem = LibraryManagerInCom.ProductList.SingleOrDefault(i=>i.id == en.product_id);
                txtTemperature.Text = en.temperature.ToString();
                txtEnthalpy.Text = en.enthalpy.ToString();
            }
        }

        public void addNew()
        {
            cbxDescription.Text = "";
            cbxDescription.IsEditable = true;

            cbxProduct.SelectedIndex = 0;
            txtTemperature.Text = "";
            txtEnthalpy.Text = "";
        }

        public Boolean saveData()
        {
            Boolean success = false;

            tb_enthalpy en = new tb_enthalpy();
            en.description = cbxDescription.Text;
            en.product_id = ((tb_product)cbxProduct.SelectedItem).id;
            en.temperature = Convert.ToInt16( txtTemperature.Text);
            en.enthalpy = Convert.ToDouble(txtEnthalpy.Text);
           
            if (en.description != "" && en.product_id >=0)
            {
                LibraryManagerInCom.addEnthalpy(en); 
                reload();
                cbxDescription.IsEditable = false;
                success = true;
            }
            else
            {
                MessageBox.Show("All value must be fill!");
                success = false;
            }

            return success;
        }

        public void delData()
        {
            LibraryManagerInCom.delEnthalpy((tb_enthalpy)cbxDescription.SelectedItem);
            reload();
        }

        public void copyData()
        {
            tb_enthalpy en = LibraryManagerInCom.copyEnthalpy((tb_enthalpy)cbxDescription.SelectedItem);
            cbxDescription.Text = en.description;
            cbxProduct.SelectedItem = LibraryManagerInCom.ProductList.SingleOrDefault(i => i.id == en.product_id);
            txtTemperature.Text = en.temperature.ToString();
            txtEnthalpy.Text = en.enthalpy.ToString();
            cbxDescription.IsEditable = true;
        }

        #region Check null value
        private void CheckEmpty(object sender, TextChangedEventArgs e)
        {
            TextBox txt = (TextBox)sender;
            if (txt.Text == "")
            {
                txt.Text = "0";
            }
            if (txt.Text.Contains(' '))
            {
                txt.Text = txt.Text.Remove(txt.Text.IndexOf(' '), 1);
                SystemSounds.Beep.Play();
            }
            if (txt.Text.First() == '.')
            {
                txt.Text = txt.Text.Remove(0, 1);
                SystemSounds.Beep.Play();
            }
        }

        #endregion

        #region CHECK INPUT
        private void txtInput_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            if (Char.IsDigit((Char)e.Text[0]))
            {
                e.Handled = false;
            }
            else
            {
                if ((Char)e.Text[0] == '.')
                {
                    if (!((TextBox)sender).Text.Contains("."))
                    {
                        e.Handled = false;
                    }
                    else
                    {
                        e.Handled = true;
                        SystemSounds.Beep.Play();
                    }
                }
                else
                {
                    e.Handled = true;
                    SystemSounds.Beep.Play();
                }
            }
        }
        #endregion
    }

}